package com.anne.fish.webapp.action;

import java.util.List;

import com.anne.fish.model.Info;
import com.anne.fish.model.InfoImage;
import com.anne.fish.service.InfoImageManager;
import com.anne.fish.service.InfoManager;
import com.opensymphony.xwork2.Preparable;

public class InfoImageAction extends BaseAction implements Preparable {
    private InfoImageManager infoImageManager;
    private List<InfoImage> infoImages;
    private InfoImage infoImage;
    private Long id;
    private String query;
    private Info info;
    
    private boolean successFlag;
    public boolean isSuccessFlag() {
		return successFlag;
	}

	public void setSuccessFlag(boolean successFlag) {
		this.successFlag = successFlag;
	}

	public Info getInfo() {
		return info;
	}

	public void setInfo(Info info) {
		this.info = info;
	}

	private InfoManager infoManager;
    
    public void setInfoManager(InfoManager infoManager) {
		this.infoManager = infoManager;
	}

	public void setInfoImageManager(InfoImageManager infoImageManager) {
        this.infoImageManager = infoImageManager;
    }

    public List getInfoImages() {
        return infoImages;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String infoImageId = getRequest().getParameter("infoImage.id");
            if (infoImageId != null && !infoImageId.equals("")) {
                infoImage = infoImageManager.get(new Long(infoImageId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        infoImages = infoImageManager.search(query, InfoImage.class);
        return SUCCESS;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public InfoImage getInfoImage() {
        return infoImage;
    }

    public void setInfoImage(InfoImage infoImage) {
        this.infoImage = infoImage;
    }

    public String delete() {
    	//add verity
    	this.infoImage = this.infoImageManager.get(id);
    	if(infoImage.getInfoId() == 0){
    		infoImageManager.remove(id);
            successFlag = true;
    	} else{
    		this.info = this.infoManager.get(infoImage.getInfoId());
    		long uid = this.getUserId();
    		if(info.getId() == uid){
    			infoImageManager.remove(id);
                successFlag = true;
    		}else{
    			successFlag = false;
    		}
    	}

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            infoImage = infoImageManager.get(id);
        } else {
            infoImage = new InfoImage();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
 

        boolean isNew = (infoImage.getId() == null);

        infoImageManager.save(infoImage);

        String key = (isNew) ? "infoImage.added" : "infoImage.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}